package Manage;

import EncapsulatingObjects.Trip;
import Util.ShoppingCartUtil;
import Util.TripDatabaseUtil;
import Util.UiUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Date;
import java.sql.SQLException;
import java.time.LocalDate;

public class AddUpdate {
    JFrame jf;
    JLabel name;
    JTextField tname;

    JLabel sPosition;
    JComboBox jsPosition;

    JLabel ePosition;
    JComboBox jePosition;

    JLabel sDate;
    JLabel syear;
    JLabel smonth;
    JLabel sday;
    JComboBox styear;
    JComboBox stmonth;
    JComboBox stday;

    JLabel eDate;
    JLabel eyear;
    JLabel emonth;
    JLabel eday;
    JComboBox etyear;
    JComboBox etmonth;
    JComboBox etday;

    JLabel money;
    JTextField tmoney;

    JLabel hotel;
    JComboBox thotel;

    JLabel details;
    JTextArea tdetails;

    JButton OK;
    JLabel reset;

    public static Trip trip = new Trip();
    public static boolean isAdd;
    UiUtil ui = new UiUtil();

    public AddUpdate() {
        init();
        ui.setFrameCenter(jf);
        ui.setFrameImage(jf);
        ui.setLogo(jf);
        if (!isAdd)
            initInfo();
        jf.setVisible(true);
    }

    private void initInfo() {
        tname.setText(trip.getName());
        jsPosition.setSelectedItem(trip.getsPosition());
        jePosition.setSelectedItem(trip.getePosition());
        String[] sdate = String.valueOf(trip.getsDate()).split("-");
        styear.setSelectedItem(Integer.parseInt(sdate[0]));
        stmonth.setSelectedItem(Integer.parseInt(sdate[1]));
        stday.setSelectedItem(Integer.parseInt(sdate[2]));
        String[] edate = String.valueOf(trip.geteDate()).split("-");
        etyear.setSelectedItem(Integer.parseInt(edate[0]));
        etmonth.setSelectedItem(Integer.parseInt(edate[1]));
        etday.setSelectedItem(Integer.parseInt(edate[2]));
        tmoney.setText(String.valueOf(trip.getMoney()));
        tdetails.setText(trip.getDoWhat());
        jf.setTitle("Update Trip");
        OK.setText("Update");
    }

    public void init() {
        jf = new JFrame("Add Trip");
        jf.setSize(700, 700);
        jf.setLayout(null);
        jf.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

        name = new JLabel("Name");
        name.setBounds(100, 50, 80, 30);
        tname = new JTextField(20);
        tname.setBounds(180, 50, 420, 30);
        jf.add(name);
        jf.add(tname);

        sPosition = new JLabel("Start Position");
        sPosition.setBounds(100, 110, 80, 30);
        jsPosition = new JComboBox(new String[]{"ShangHai", "BeiJing", "ChengDu", "XinJiang", "ChongQing", "YunNan","HaiNan",
                "QingDao",  "GanSu", "NingXia", "GuangZhou", "Harbin", "NewYork", "ShenZhen", "Xi'an", "YingChuan", "Mohe",
                "Lasa", "Oslo", "Copenhagen", "Maldives", "Italy", "Zhongwei", "Tengger Desert", "YanTai", "TaiWan",
                "HongKong", "AoMen", "NanJing", "WuHan"});
        jsPosition.setBounds(180, 110, 150, 25);
        jf.add(sPosition);
        jf.add(jsPosition);

        ePosition = new JLabel("End Position");
        ePosition.setBounds(370, 110, 80, 25);
        jePosition = new JComboBox(new String[]{"ShangHai", "BeiJing", "ChengDu", "XinJiang", "ChongQing", "YunNan","HaiNan",
                "QingDao",  "GanSu", "NingXia", "GuangZhou", "Harbin", "NewYork", "ShenZhen", "Xi'an", "YingChuan", "Mohe",
                "Lasa", "Oslo", "Copenhagen", "Maldives", "Italy", "Zhongwei", "Tengger Desert", "YanTai", "TaiWan",
                "HongKong", "AoMen", "NanJing", "WuHan"});
        jePosition.setBounds(450, 110, 150, 25);
        jf.add(ePosition);
        jf.add(jePosition);

        sDate = new JLabel("Start Date");
        sDate.setBounds(100, 170, 80, 25);
        styear = new JComboBox();
        styear.setBounds(180, 170, 80, 25);
        syear = new JLabel("Year");
        syear.setBounds(265, 170, 50, 25);
        stmonth = new JComboBox();
        stmonth.setBounds(325, 170, 80, 25);
        smonth = new JLabel("Month");
        smonth.setBounds(410, 170, 50, 25);
        stday = new JComboBox();
        stday.setBounds(490, 170, 80, 25);
        sday = new JLabel("Day");
        sday.setBounds(575, 170, 50, 25);
        styear.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                updataDay(jf, styear, stmonth, stday);
            }
        });
        stmonth.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                updataDay(jf, styear, stmonth, stday);
            }
        });
        jf.add(sDate);
        jf.add(styear);
        jf.add(syear);
        jf.add(stmonth);
        jf.add(smonth);
        jf.add(stday);
        jf.add(sday);
        updateTime(styear, stmonth, stday);

        eDate = new JLabel("End Date");
        eDate.setBounds(100, 230, 80, 25);
        etyear = new JComboBox();
        etyear.setBounds(180, 230, 80, 25);
        eyear = new JLabel("Year");
        eyear.setBounds(265, 230, 50, 25);
        etmonth = new JComboBox();
        etmonth.setBounds(325, 230, 80, 25);
        emonth = new JLabel("Month");
        emonth.setBounds(410, 230, 50, 25);
        etday = new JComboBox();
        etday.setBounds(490, 230, 80, 25);
        eday = new JLabel("Day");
        eday.setBounds(575, 230, 50, 25);
        etyear.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                updataDay(jf, etyear, etmonth, etday);
            }
        });
        etmonth.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                updataDay(jf, etyear, etmonth, etday);
            }
        });
        jf.add(eDate);
        jf.add(etyear);
        jf.add(eyear);
        jf.add(etmonth);
        jf.add(emonth);
        jf.add(etday);
        jf.add(eday);

        updateTime(etyear, etmonth, etday);

        money = new JLabel("Money");
        money.setBounds(100, 285, 80, 30);
        tmoney = new JTextField(20);
        tmoney.setBounds(180, 285, 420, 30);
        jf.add(money);
        jf.add(tmoney);

        hotel = new JLabel("Hotel");
        hotel.setBounds(100, 345, 360, 25);
        thotel = new JComboBox(new String[]{"City Celebrity Hotel","MaoChengXuan Hotel",
                "Xinqiao Hotel", "The Best Western wealth Hotel", "Shu Xin Yuan Hotel", "Mansion Hotel",
                "Yuebei Hotel","Arctic Village Hotel","Renqing Hotel","YuanMengYa Hotel","Jun Zhi Ru Gui Hotel",
                "XiangLong Hotel","YiQing Hotel","XiangLing Hotel","XiaJing Hotel","Four Seasons Hotel",
                "Waldorf Hotel","Holiday Inn Hotel","InterContinental Hotel","Newman Hotel",
                "Padan Hotel","City Celebrity Hotel","Royal post","Deer crowing springHotel"});
        thotel.setBounds(180, 345, 150, 25);
        jf.add(hotel);
        jf.add(thotel);

        details = new JLabel("Details", JLabel.CENTER);
        details.setBounds(100, 385, 500, 30);
        tdetails = new JTextArea();
        tdetails.setBounds(100, 420, 500, 100);
        tdetails.setLineWrap(true);
        jf.add(details);
        jf.add(tdetails);

        OK = new JButton("OK");
        OK.setBounds(250, 560, 200, 50);
        OK.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                submitInfo();
            }
        });
        jf.add(OK);

        reset = new JLabel("Reset");
        reset.setBounds(500, 572, 50, 30);
        reset.setForeground(Color.gray);
        reset.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        reset.setFont(new Font("Harlow Solid Italic", Font.BOLD, 16));
        reset.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                clearAllInput();
            }
        });
        jf.add(reset);

        jf.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if (JOptionPane.showConfirmDialog(jf, "Are you sure you want to exit?") == JOptionPane.YES_OPTION) {
                    jf.dispose();
                }
            }
        });
    }

    void initTrip(Trip trip) {
        trip.setName(tname.getText().trim());
        trip.setsPosition((String) jsPosition.getSelectedItem());
        trip.setePosition((String) jePosition.getSelectedItem());
        String sd = styear.getSelectedItem() + "-" + stmonth.getSelectedItem() + "-" + stday.getSelectedItem();
        trip.setsDate(Date.valueOf(sd));
        String ed = etyear.getSelectedItem() + "-" + etmonth.getSelectedItem() + "-" + etday.getSelectedItem();
        trip.seteDate(Date.valueOf(ed));
        trip.setMoney(Double.parseDouble(tmoney.getText()));
        trip.setHotelNamex((String) thotel.getSelectedItem());
        trip.setDoWhat(tdetails.getText().trim());
    }

    private void submitInfo() {
        if (isAdd) {//添加
            Trip trip = new Trip();
            initTrip(trip);
            TripDatabaseUtil td = new TripDatabaseUtil();
            try {
                if (td.insertInfo(trip))
                    if (JOptionPane.showConfirmDialog(jf, "Information added successfully, do you want to continue adding") == JOptionPane.YES_OPTION)
                        clearAllInput();
                    else {
                        jf.dispose();
                    }
                ShoppingCartUtil su = new ShoppingCartUtil();
                su.getTripId(trip);
                su.addTrips(trip);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } else {
            TripDatabaseUtil dp = new TripDatabaseUtil();
            initTrip(trip);
            dp.updateTrip(trip);
            JOptionPane.showMessageDialog(jf, "Change succeed");
        }
    }

    private void clearAllInput() {
        tname.setText("");
        jsPosition.setSelectedIndex(0);
        jePosition.setSelectedIndex(0);
        styear.setSelectedIndex(0);
        stmonth.setSelectedIndex(0);
        stday.setSelectedIndex(0);
        etyear.setSelectedIndex(0);
        etmonth.setSelectedIndex(0);
        etday.setSelectedIndex(0);
        tmoney.setText("");
        tdetails.setText("");
    }

    private void updataDay(JFrame jf, JComboBox tyear, JComboBox tmonth, JComboBox tday) {

        int years = 1950;
        int months = 1;
        int days = 0;
        Object oYear = tyear.getSelectedItem();

        Object oMonth = tmonth.getSelectedItem();
        if (oYear == null || oMonth == null) {
            return;
        } else {
            years = (int) oYear;
            months = (int) oMonth;
        }

        switch (months) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                if ((years % 4 == 0 && years % 100 != 0) || years % 400 == 0) {
                    days = 29;
                } else {
                    days = 28;
                }
                break;
        }
        tday.removeAllItems();
        for (int i = 1; i <= days; i++)
            tday.addItem(i);
    }

    void updateTime(JComboBox tyear, JComboBox tmonth, JComboBox tday) {
        int year = LocalDate.now().getYear();
        for (int i = year; i <= year + 5; i++) {
            tyear.addItem(i);
        }
        for (int i = 1; i <= 12; i++) {
            tmonth.addItem(i);
        }
        updataDay(jf, tyear, tmonth, tday);
    }
}
